#include "BasicEvent.h"

namespace events {


EventFactory *		EventFactory::_instance = NULL;
boost::once_flag	EventFactory::_instance_flag;


HANDLE BasicEvent::_hHeap = ::HeapCreate(0, 0, 0);


void BasicEvent::toStream(std::ostream &os) {
	os.write(reinterpret_cast<const char*>(&_etype), sizeof(_etype));
	_timestamp.saveToStream(os);
	saveToStream(os);
}

/*static*/
BasicEvent * BasicEvent::fromStream(std::istream &is) {
	EventTypes etype;
	is.read(reinterpret_cast<char*>(&etype), sizeof(etype));
	if (is.gcount() != sizeof(etype)) return NULL;
	if (etype < 0 || etype >= BasicEvent::_num_EventTypes)
		THROW(platform::SerializationException, "unexpected event type"); 

	BasicEvent *newEv = EventFactory::GetInstance().allocate(etype);
	if (newEv == NULL) return NULL;
	newEv->_timestamp.readFromStream(is);
	newEv->readFromStream(is);
	return newEv;
}


EventFactory::EventFactory(void) {
	for (int i = 0; i < BasicEvent::_num_EventTypes; i++)
		_allocators[i] = new NullAllocator();
}



}  // end namespace
